#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
using namespace std;
const int N = 210;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
struct Point {
	int x,y;
	Point(){}
	Point(int _x,int _y):x(_x),y(_y){}
};
int t,n,m,d[N][N],sx,sy,ex,ey;
char s[N][N];

int main()
{
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d%d",&n,&m);
		for (int i=0;i<n;i++) scanf("%s",s[i]);
		for (int i=0;i<n;i++) 
			for (int j=0;j<m;j++) 
				if (s[i][j]=='S')
					sx=i,sy=j;
				else
				if (s[i][j]=='E')
					ex=i,ey=j;
		queue<Point> Q;
		Q.push(Point(sx,sy));
		memset(d,127,sizeof d);
		d[sx][sy]=0;
		while (!Q.empty())
		{
			Point t=Q.front();
			Q.pop();
			for (int i=0;i<4;i++) 
			{
				int tx=t.x+dx[i], ty=t.y+dy[i];
				if (tx<0||ty<0||tx>=n||ty>=m||s[tx][ty]=='#') continue;
				if (d[tx][ty]>d[t.x][t.y]+1)
					d[tx][ty]=d[t.x][t.y]+1, Q.push(Point(tx,ty));
			}
		}
		if (d[ex][ey]>0x7fffff) puts("oop!");
		else printf("%d\n",d[ex][ey]);
	}
	return 0;
}